package com.ruoyi.common.utils.android;

import java.math.BigInteger;

/* loaded from: classes.dex */
public class a extends b {
    static final byte[] k = {13, 10};
    private static final byte[] l = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47};
    private static final byte[] m = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95};
    private static final byte[] n = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
    private final byte[] f;
    private final byte[] g;
    private final byte[] h;
    private final int i;
    private final int j;

    public a() {
        this(0);
    }

    public static byte[] decodeBase64(String str) {
        return new com.ruoyi.common.utils.android.a().decode(str);
    }

    public static BigInteger decodeInteger(byte[] bArr) {
        return new BigInteger(1, decodeBase64(bArr));
    }

    public static byte[] encodeBase64(byte[] bArr) {
        return encodeBase64(bArr, false);
    }

    public static byte[] encodeBase64Chunked(byte[] bArr) {
        return encodeBase64(bArr, true);
    }

    public static String encodeBase64String(byte[] bArr) {
        return II.newStringUtf8(encodeBase64(bArr, false));
    }

    public static byte[] encodeBase64URLSafe(byte[] bArr) {
        return encodeBase64(bArr, false, true);
    }

    public static String encodeBase64URLSafeString(byte[] bArr) {
        return II.newStringUtf8(encodeBase64(bArr, false, true));
    }

    public static byte[] encodeInteger(BigInteger bigInteger) {
        if (bigInteger != null) {
            return encodeBase64(a(bigInteger), false);
        }
        throw new NullPointerException("encodeInteger called with null parameter");
    }

    @Deprecated
    public static boolean isArrayByteBase64(byte[] bArr) {
        return isBase64(bArr);
    }

    public static boolean isBase64(byte b) {
        if (b != 61) {
            if (b >= 0) {
                byte[] bArr = n;
                if (b >= bArr.length || bArr[b] == -1) {
                }
            }
            return false;
        }
        return true;
    }

    @Override // com.md.obj.binary.decrypt.b
    void a(byte[] bArr, int i, int i2, b.a aVar) {
        byte b;
        if (aVar.f) {
            return;
        }
        if (i2 < 0) {
            aVar.f = true;
        }
        int i3 = 0;
        while (true) {
            if (i3 >= i2) {
                break;
            }
            byte[] a = a(this.i, aVar);
            int i4 = i + 1;
            byte b2 = bArr[i];
            if (b2 == this.a) {
                aVar.f = true;
                break;
            }
            if (b2 >= 0) {
                byte[] bArr2 = n;
                if (b2 < bArr2.length && (b = bArr2[b2]) >= 0) {
                    aVar.h = (aVar.h + 1) % 4;
                    aVar.a = (aVar.a << 6) + b;
                    if (aVar.h == 0) {
                        int i5 = aVar.f954d;
                        aVar.f954d = i5 + 1;
                        int i6 = aVar.a;
                        a[i5] = (byte) ((i6 >> 16) & 255);
                        int i7 = aVar.f954d;
                        aVar.f954d = i7 + 1;
                        a[i7] = (byte) ((i6 >> 8) & 255);
                        int i8 = aVar.f954d;
                        aVar.f954d = i8 + 1;
                        a[i8] = (byte) (i6 & 255);
                    }
                }
            }
            i3++;
            i = i4;
        }
        if (!aVar.f || aVar.h == 0) {
            return;
        }
        byte[] a2 = a(this.i, aVar);
        int i9 = aVar.h;
        if (i9 == 1) {
            return;
        }
        if (i9 == 2) {
            aVar.a >>= 4;
            int i10 = aVar.f954d;
            aVar.f954d = i10 + 1;
            a2[i10] = (byte) (aVar.a & 255);
        } else if (i9 == 3) {
            aVar.a >>= 2;
            int i11 = aVar.f954d;
            aVar.f954d = i11 + 1;
            int i12 = aVar.a;
            a2[i11] = (byte) ((i12 >> 8) & 255);
            int i13 = aVar.f954d;
            aVar.f954d = i13 + 1;
            a2[i13] = (byte) (i12 & 255);
        } else {
            throw new IllegalStateException("Impossible modulus " + aVar.h);
        }
    }

    @Override // com.md.obj.binary.decrypt.b
    void b(byte[] bArr, int i, int i2, b.a aVar) {
        if (aVar.f) {
            return;
        }
        if (i2 >= 0) {
            int i3 = i;
            int i4 = 0;
            while (i4 < i2) {
                byte[] a = a(this.j, aVar);
                aVar.h = (aVar.h + 1) % 3;
                int i5 = i3 + 1;
                int i6 = bArr[i3];
                if (i6 < 0) {
                    i6 += 256;
                }
                aVar.a = (aVar.a << 8) + i6;
                if (aVar.h == 0) {
                    int i7 = aVar.f954d;
                    aVar.f954d = i7 + 1;
                    byte[] bArr2 = this.f;
                    int i8 = aVar.a;
                    a[i7] = bArr2[(i8 >> 18) & 63];
                    int i9 = aVar.f954d;
                    aVar.f954d = i9 + 1;
                    a[i9] = bArr2[(i8 >> 12) & 63];
                    int i10 = aVar.f954d;
                    aVar.f954d = i10 + 1;
                    a[i10] = bArr2[(i8 >> 6) & 63];
                    int i11 = aVar.f954d;
                    aVar.f954d = i11 + 1;
                    a[i11] = bArr2[i8 & 63];
                    aVar.g += 4;
                    int i12 = this.f951d;
                    if (i12 > 0 && i12 <= aVar.g) {
                        byte[] bArr3 = this.h;
                        System.arraycopy(bArr3, 0, a, aVar.f954d, bArr3.length);
                        aVar.f954d += this.h.length;
                        aVar.g = 0;
                    }
                }
                i4++;
                i3 = i5;
            }
            return;
        }
        aVar.f = true;
        if (aVar.h == 0 && this.f951d == 0) {
            return;
        }
        byte[] a2 = a(this.j, aVar);
        int i13 = aVar.f954d;
        int i14 = aVar.h;
        if (i14 != 0) {
            if (i14 == 1) {
                aVar.f954d = i13 + 1;
                byte[] bArr4 = this.f;
                int i15 = aVar.a;
                a2[i13] = bArr4[(i15 >> 2) & 63];
                int i16 = aVar.f954d;
                aVar.f954d = i16 + 1;
                a2[i16] = bArr4[(i15 << 4) & 63];
                if (bArr4 == l) {
                    int i17 = aVar.f954d;
                    aVar.f954d = i17 + 1;
                    byte b = this.a;
                    a2[i17] = b;
                    int i18 = aVar.f954d;
                    aVar.f954d = i18 + 1;
                    a2[i18] = b;
                }
            } else if (i14 == 2) {
                aVar.f954d = i13 + 1;
                byte[] bArr5 = this.f;
                int i19 = aVar.a;
                a2[i13] = bArr5[(i19 >> 10) & 63];
                int i20 = aVar.f954d;
                aVar.f954d = i20 + 1;
                a2[i20] = bArr5[(i19 >> 4) & 63];
                int i21 = aVar.f954d;
                aVar.f954d = i21 + 1;
                a2[i21] = bArr5[(i19 << 2) & 63];
                if (bArr5 == l) {
                    int i22 = aVar.f954d;
                    aVar.f954d = i22 + 1;
                    a2[i22] = this.a;
                }
            } else {
                throw new IllegalStateException("Impossible modulus " + aVar.h);
            }
        }
        int i23 = aVar.g;
        int i24 = aVar.f954d;
        aVar.g = i23 + (i24 - i13);
        if (this.f951d <= 0 || aVar.g <= 0) {
            return;
        }
        byte[] bArr6 = this.h;
        System.arraycopy(bArr6, 0, a2, i24, bArr6.length);
        aVar.f954d += this.h.length;
    }

    public boolean isUrlSafe() {
        return this.f == m;
    }

    public a(boolean z) {
        this(76, k, z);
    }

    public static byte[] decodeBase64(byte[] bArr) {
        return new com.ruoyi.common.utils.android.a().decode(bArr);
    }

    public static byte[] encodeBase64(byte[] bArr, boolean z) {
        return encodeBase64(bArr, z, false);
    }

    public static boolean isBase64(String str) {
        return isBase64(II.getBytesUtf8(str));
    }

    public a(int i) {
        this(i, k);
    }

    public static byte[] encodeBase64(byte[] bArr, boolean z, boolean z2) {
        return encodeBase64(bArr, z, z2, Integer.MAX_VALUE);
    }

    public static boolean isBase64(byte[] bArr) {
        for (int i = 0; i < bArr.length; i++) {
            if (!isBase64(bArr[i]) && !b.b(bArr[i])) {
                return false;
            }
        }
        return true;
    }

    public a(int i, byte[] bArr) {
        this(i, bArr, false);
    }

    public static byte[] encodeBase64(byte[] bArr, boolean z, boolean z2, int i) {
        if (bArr == null || bArr.length == 0) {
            return bArr;
        }
        com.ruoyi.common.utils.android.a aVar = z ? new com.ruoyi.common.utils.android.a(z2) : new com.ruoyi.common.utils.android.a(0, k, z2);
        long encodedLength = aVar.getEncodedLength(bArr);
        if (encodedLength <= i) {
            return aVar.encode(bArr);
        }
        throw new IllegalArgumentException("Input array too big, the output array would be bigger (" + encodedLength + ") than the specified maximum size of " + i);
    }

    public a(int i, byte[] bArr, boolean z) {
        super(3, 4, i, bArr == null ? 0 : bArr.length);
        this.g = n;
        if (bArr != null) {
            if (a(bArr)) {
                String newStringUtf8 = II.newStringUtf8(bArr);
                throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + newStringUtf8 + "]");
            } else if (i > 0) {
                this.j = bArr.length + 4;
                this.h = new byte[bArr.length];
                System.arraycopy(bArr, 0, this.h, 0, bArr.length);
            } else {
                this.j = 4;
                this.h = null;
            }
        } else {
            this.j = 4;
            this.h = null;
        }
        this.i = this.j - 1;
        this.f = z ? m : l;
    }

    static byte[] a(BigInteger bigInteger) {
        int bitLength = ((bigInteger.bitLength() + 7) >> 3) << 3;
        byte[] byteArray = bigInteger.toByteArray();
        if (bigInteger.bitLength() % 8 == 0 || (bigInteger.bitLength() / 8) + 1 != bitLength / 8) {
            int i = 0;
            int length = byteArray.length;
            if (bigInteger.bitLength() % 8 == 0) {
                length--;
                i = 1;
            }
            int i2 = bitLength / 8;
            int i3 = i2 - length;
            byte[] bArr = new byte[i2];
            System.arraycopy(byteArray, i, bArr, i3, length);
            return bArr;
        }
        return byteArray;
    }

    @Override // com.md.obj.binary.decrypt.b
    protected boolean a(byte b) {
        if (b >= 0) {
            byte[] bArr = this.g;
            if (b < bArr.length && bArr[b] != -1) {
                return true;
            }
        }
        return false;
    }
}
